package com.alibaba.aliper.client.model;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.alibaba.aliper.client.view.IView;
import com.alibaba.aliper.server.interfaces.AliperRemoteView;
import com.alibaba.aliper.server.model.domain.Point;
import com.caucho.hessian.client.HessianProxyFactory;

public class AliperClientModel implements IModelAccess , Runnable{

	private IView view;
	private String serviceUrl;
	private long interval = 100;
	public void refresh() {
		this.initCatlog();
	}
	public synchronized void setView(IView view){
		this.view = view;
	}
	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}
	private AliperRemoteView remoteView = null;
	private boolean running;
	public synchronized void connect() throws MalformedURLException {
		HessianProxyFactory factory = new HessianProxyFactory();
		remoteView = (AliperRemoteView) factory.create(AliperRemoteView.class, this.serviceUrl);
		initCatlog();
		if(!this.running){
			this.running = true;
			new Thread(this,"aliperClientModelThread").start();
		}
	}
	@Override
	public void run() {
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(this.running){
			try {
				Thread.sleep(this.interval);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.fetchChartData();
		}
	}
	private synchronized void initCatlog() {
		this.linespecMap = new HashMap<String,Integer>();
		if(this.remoteView == null){
			JOptionPane.showMessageDialog((JFrame)this.view, "未连接服务器","出错了",JOptionPane.ERROR_MESSAGE);
			return;
		}
		String[] agents = this.remoteView.getAgents(this.project);
		if(agents == null){
			return;
		}
		for(String agent : agents){
			String[] charts = this.remoteView.getCharts(this.project,agent);
			if(charts == null){
				continue;
			}
			for(String chart : charts){
				String[] lines = this.remoteView.getLines(this.project,agent, chart);
				if(lines == null){
					continue;
				}
				for(String line : lines){
					this.linespecMap.put(agent + "$$" + chart + "$$"+ line,0);
				}
			}
		}
	}
	private HashMap<String,Integer> linespecMap = null;
	private synchronized void fetchChartData()  {
		for(String element : this.linespecMap.keySet()){
			String[] lineSpec = element.split("\\$\\$");
			if(lineSpec.length >= 3){
				String agent = lineSpec[0];
				String chart = lineSpec[1];
				String line = lineSpec[2];
				try {
					int index = this.linespecMap.get(element);
					Point[] points = this.remoteView.getPointsAfter(this.project,agent, chart, line,index);
					if(points != null){
						for(Point p : points){
							this.view.fill(agent, chart, line, p.getX(), p.getY());
						}
						// update index for this line of chart
						index += points.length;
						this.linespecMap.put(element, index);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Override
	public String[] getChartSpecfics() {
		Set<String> set =  this.linespecMap.keySet();
		String[] array = set.toArray(new String[set.size()]);
		return array;
	}
	@Override
	public synchronized void restart() {
		this.remoteView.restart(this.project);
	}
	private String project = null;
	@Override
	public synchronized void setProject(String project) {
		this.project = project;
	}
	@Override
	public String getServiceURL() {
		return this.serviceUrl;
	}
	@Override
	public String[] getProjects(String url) throws MalformedURLException {
		HessianProxyFactory factory = new HessianProxyFactory();
		AliperRemoteView remoteView = (AliperRemoteView) factory.create(AliperRemoteView.class,url);
		return remoteView.getPojects();
	}

}
